package com.jetpackframework.ftp;

import android.text.TextUtils;

import com.jetpackframework.request.RequestUtil;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FtpUtil extends RequestUtil<FTPTask,FtpCache> {
    private static FtpUtil ftpUtil;
    private FtpUtil(){
        super();
    }
    public static synchronized FtpUtil getInstance(){
        if (ftpUtil == null){
            ftpUtil = new FtpUtil();
        }
        return ftpUtil;
    }

    /**
     * ftp上传
     * @return
     */
    private static String ftpUpload(FTPTask task){
        FTPClient ftpClient = new FTPClient();
        FileInputStream fis = null;
        String returnMessage = "0";
        try {
            ftpClient.connect(task.ftpUrl, task.port);
            boolean loginResult = ftpClient.login(task.username, task.password);
            int returnCode = ftpClient.getReplyCode();
            if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登录成功
                ftpClient.setBufferSize(1024);
                ftpClient.setControlEncoding("UTF-8");
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                ftpClient.enterLocalPassiveMode();
                fis = new FileInputStream(task.filePath + task.fileName);
                ftpClient.storeFile(task.fileName, fis);

                returnMessage = "1";   //上传成功
            } else {// 如果登录失败
                returnMessage = "0";
            }

        } catch (IOException e) {
            e.printStackTrace();
//		     throw new RuntimeException("FTP客户端出错！", e);
            returnMessage = "-1";
        } finally {
            //IOUtils.closeQuietly(fis);
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("关闭FTP连接发生异常！", e);
            }
        }
        return returnMessage;
    }

    /**
     * ftp下载
     */
    private static String ftpDown(FTPTask task){
        BufferedOutputStream buffOut = null;

        FTPClient ftpClient = new FTPClient();
        String returnMessage = "0";
        try {
            ftpClient.connect(task.ftpUrl, task.port);
            boolean loginResult = ftpClient.login(task.username, task.password);
            int returnCode = ftpClient.getReplyCode();
            if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登录成功
                ftpClient.setBufferSize(1024);
                ftpClient.setControlEncoding("UTF-8");
                ftpClient.enterLocalPassiveMode();

                buffOut = new BufferedOutputStream(new FileOutputStream(task.filePath+task.fileName),8*1024);
                ftpClient.retrieveFile(task.ftpFile, buffOut);
                buffOut.flush();
                buffOut.close();

                ftpClient.logout();
                ftpClient.disconnect();

                returnMessage = "1";   //上传成功
            } else {// 如果登录失败
                returnMessage = "0";
            }

        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错！", e);
        } finally {
            //IOUtils.closeQuietly(fis);
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("关闭FTP连接发生异常！", e);
            }
        }
        return returnMessage;
    }

    @Override
    protected void execTask(FTPTask task) {
        if (TextUtils.isEmpty(task.ftpFile)){
            ftpUpload(task);
        }else {
            ftpDown(task);
        }
    }

    @Override
    public FtpCache getCache() {
        return FtpCache.getCache();
    }
}
